package com.example.backend.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.backend.entity.PeiSong;
import com.example.backend.entity.vo.ShowQs;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository // 表示持久层
public interface PeiSongMapper extends BaseMapper<PeiSong> {

    /**
     * 根据订单id查询配送信息
     */
    @Select("SELECT * FROM wm_ps INNER JOIN wm_dd ON wm_ps.ddid = wm_dd.ddid WHERE wm_dd.sjid = #{sjid} AND DATE(wm_dd.ddsj) = CURDATE() AND wm_ps.pszt = 0")
    List<PeiSong> queryWPeiSongByYhid(Integer sjid);

    /**
     * 根据订单id查询配送信息
     */
    @Select("SELECT * FROM wm_ps INNER JOIN wm_dd ON wm_ps.ddid = wm_dd.ddid WHERE wm_dd.sjid = #{sjid} AND DATE(wm_dd.ddsj) = CURDATE() AND wm_ps.pszt = 1")
    List<PeiSong> queryPeiSongZByYhid(Integer sjid);

    /**
     * 根据订单id查询配送信息
     */
    @Select("SELECT * FROM wm_ps INNER JOIN wm_dd ON wm_ps.ddid = wm_dd.ddid WHERE wm_dd.sjid = #{sjid} AND DATE(wm_dd.ddsj) = CURDATE() AND wm_ps.pszt = 2")
    List<PeiSong> queryYiSongDaByYhid(Integer sjid);

    /**
     * 根据订单id更新配送状态
     */
    @Update("update wm_ps set pszt = #{pszt} where ddid = #{ddid}")
    int updateByDdId(PeiSong peiSong);

    /**
     * 查询所有已出餐的配送表信息
     */
    @Select("SELECT ps.psid,ps.pszt,dd.ddid,sj.name,sf.yhnc,sf.phone,dd.dddz FROM wm_ps ps JOIN wm_dd dd ON ps.ddid = dd.ddid JOIN wm_sj sj on dd.sjid = sj.sjid join wm_sf sf on dd.yhid = sf.yhid where pszt = 3")
    List<ShowQs> queryPsWithPszt();

    /**
     * 通过骑手id查询自己在配送中的配送表信息
     */
    @Select("SELECT ps.psid,ps.pszt,dd.ddid,sj.name,sf.yhnc,sf.phone,dd.dddz FROM wm_ps ps JOIN wm_dd dd ON ps.ddid = dd.ddid JOIN wm_sj sj on dd.sjid = sj.sjid join wm_sf sf on dd.yhid = sf.yhid where pszt = 6 and qsid = #{qsid}")
    List<ShowQs> queryPsing(Integer qsid);

    /**
     * 通过骑手id查询自己所有已完成的配送表信息
     */
    @Select("SELECT ps.psid,ps.pszt,dd.ddid,sj.name,sf.yhnc,sf.phone,dd.dddz FROM wm_ps ps JOIN wm_dd dd ON ps.ddid = dd.ddid JOIN wm_sj sj on dd.sjid = sj.sjid join wm_sf sf on dd.yhid = sf.yhid where pszt = 9 and qsid = #{qsid}")
    List<ShowQs> querySuccess(Integer qsid);
}
